import AbstractPlatform from './AbstractPlatform'
import Help from './help/Help'
import { Collapse } from './control/Collapse'
import WebMap from './WebMap'
import { queryService } from '@supermap/iclient-leaflet'
/**
 * @classdesc 延迟创建Map
 * @author Niyang
 */
export default class Platform extends AbstractPlatform {
    constructor () {
        super()
        this.proxy = null
    }

    /**
     * @description 创建地图
     * @param {HTMLElement | Strring} el
     * @param {Object} options 地图参数
     * @param {Function} onReady 地图加载完后回调函数
     */
    build (el, options, onReady) {
        this.proxy = new WebMap(el, options)
        this.proxy.whenReady(() => {
            onReady.call(this, this)
        })
    }

    /**
     *
     * @param {Object | true} cfg
     */
    reszie (cfg) {
        this.proxy.reszie(cfg)
    }

    /**
     *
     * @param {Object} options
     * @returns Layer
     */
    layer (options) {
        return this.proxy.buildLayer(options)
    }

    /**
     * @description 创建Marker
     * @param {Object} options
     * @returns Marker
     */
    marker (options) {
        return this.proxy.buildMarker(options)
    }

    /**
     * @description 获取地图实例
     * @returns Map
     */
    map () {
        return this.proxy.getMap()
    }

    /**
     * @description 获取FeatureLayerGroup
     * @returns FeatureLayerGroup
     */
    feature () {
        return this.proxy.getFeature()
    }

    /**
         * @description 创建折叠Control
         * @param {Object} options
         */
    collapse (options) {
        const control = new Collapse(options)
        control.addTo(this.map())
    }

    /**
     *
     * @param {*} feature
     * @param {*} options
     * @returns
     */
    geoJSON (feature, options) {
        return this.proxy.getLayerFromGeoJSON(feature, options)
    }

    /**
     * @description 获取缩放
     * @returns Number
     */
    getZoom () {
        return this.proxy.getZoom()
    }

    /**
     * @description 获取全部特征Layer
     * @returns Array<Layer>
     */
    getLayers () {
        return this.proxy.getFeatureLayers()
    }

    /**
     * @description  获取最新的Layer
     * @returns Layer
     */
    getActivedLayer () {
        return this.proxy.getLatestFeatureLayer()
    }

    /**
     * @description 地图添加Layer
     * @param {Layer} layer
     * @param {Object} fitOptions
     */
    addLayer (layer, fitOptions) {
        this.proxy.addLayer(layer)
        if (fitOptions) { this.fitBounds(layer.getBounds(), fitOptions) }
    }

    /**
     * @description 获取Layer
     * @param {String} id
     * @returns  Layer
     */
    getLayer (id) {
        return this.proxy.getLayer(id)
    }

    /**
     * @description 保存当前的LayerId
     * @param {String} id
     * @param {Number|undefined} layerId
     */
    keep (id, layerId) {
        const layerid = layerId || this.getActivedLayer()._leaflet_id
        this.proxy.put(id, layerid)
    }

    /**
     *@description 通过配置添加Layer
     */
    addLayerFromJSON (options, fitOptions) {
        this.addLayer(this.layer(options), fitOptions)
    }

    /**
     * @description 移除Layer
     * @param {Layer} layer
     */
    removeLayer (layer) {
        this.proxy.removeLayer(layer)
    }

    /**
     * @description 删除某个Layer
     * @param {String} id
     */
    removeLayerById (id) {
        this.removeLayer(this.getLayer(id))
    }

    /**
     * @description 添加特征图层
     * @param {Layer} layer
     * @param {Boolean} only
     * @param {Object|false} fitOptions
     */
    addFeatureLayer (layer, only, fitOptions) {
        this.proxy.addFeatureLayer(layer, only)
        if (fitOptions) { this.fitBounds(this.feature().getBounds(), fitOptions) }
    }

    /**
     * @description 添加特征图层
     * @param {JSON} features
     * @param {Object} options
     * @param {Boolean} only
     */
    addFeatureLayerFromGeoJSON (features, options, only, fitOptions) {
        this.addFeatureLayer(this.geoJSON(features, options), only, fitOptions)
    }

    /**
     *
     * @param {*} options
     * @param {*} only
     * @param {*} fitOptions
     */
    addMarkerLayerFromJSON (options, only, fitOptions) {
        this.addFeatureLayer(this.marker(options), only, fitOptions)
    }

    /**
     * @description 移除Layer
     * @param {Layer} layer
     */
    removeFeatureLayer (layer) {
        this.proxy.removeFeatureLayer(layer)
    }

    /**
     * @description 清除所有的Layer
     */
    clearFeatureLayers () {
        this.proxy.clearFeatureLayers()
    }

    /**
     *
     * @param {Object} bounds
     * @param {Object} options
     */
    fitBounds (bounds, options) {
        this.proxy.fitBounds(bounds, options)
    }

    /**
     *
     * @param {Array<Number>} center
     * @param {Number} zoom
     * @param {Object} options
     */
    setView (center, zoom, options) {
        this.proxy.setView(center, zoom, options)
    }

    /**
     * @description 飞到某个点
     * @param {LatLng} center
     * @param {Number} zoom
     * @param {Object} options
     */
    flyTo (center, zoom, options) {
        this.proxy.flyTo(center, zoom, options)
    }

    /**
     * @description 销毁
     */
    destroy () {
        if (this.proxy) {
            this.proxy.destroy()
        }
    }

    /**
     * @description 自定义Marker的Icon
     * @returns Help类
     */
    help () {
        return Help.getHelp()
    }

    query (url) {
        return queryService(url)
    }

    on (name, fn, context) {
        this.proxy.on(name, fn, context)
    }

    off (name, fn, context) {
        this.proxy.off(name, fn, context)
    }

    emit (name, ...args) {
        this.proxy.emit(name, ...args)
    }
}
